﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.Model;
using FantastPlatform.Framework.Service;
using FantastPlatform.Community.IService;
using NHibernate;
using NHibernate.Criterion;

namespace FantastPlatform.Community.Service
{
    public class HouseHoldService : DataService<IHouseHold, HouseHold>, IHouseHoldService
    {
        private PersonService personService = new PersonService();
        private BusinessPersonService businessPersonServie = new BusinessPersonService();
        public IList<IHouseHold> GetHouseHoldByHouseDistrictId(int houseDistrictId)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(HouseHold));
            criteria.Add(Expression.Eq("HouseDistrict.Id", houseDistrictId)).Add(Expression.Eq("Enable", true));
            IList<IHouseHold> houseHoldList = criteria.List<IHouseHold>();
            return houseHoldList;
        }

        public IHouseHold GetHouseHoldInfoByNumber(string number)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(HouseHold));
            criteria.Add(Expression.Eq("Number", number)).Add(Expression.Eq("Enable", true));
            IHouseHold houseHold = criteria.List<IHouseHold>().SingleOrDefault();
            return houseHold;
        }

        #region IHouseHoldService 成员
        public void SavehouseHold(IHouseHold houseHold)
        {
            if (houseHold.Id == 0)
            {
                foreach (IPerson person in houseHold.Persons)
                {
                    person.CreateDate = DateTime.Now;
                    person.Enable = true;
                    personService.Session.Save(person);
                }
                houseHold.Enable = true;
                houseHold.CreateDate = DateTime.Now;
                base.SaveOrUpdate(houseHold);
            }
            else
            {
                foreach (IPerson person in houseHold.Persons)
                {
                    if (person.Id == 0)
                    {
                        person.CreateDate = DateTime.Now;
                        person.Enable = true;
                        personService.Session.Save(person);
                    }
                    else
                    {
                        person.ModifyDate = DateTime.Now;
                        personService.Session.Update(person);
                    }
                }
                houseHold.Enable = true;
                houseHold.ModifyDate = DateTime.Now;
                base.SaveOrUpdate(houseHold);
            }
        }

        #endregion


        public object[] SearchByCondition(string houseDistrictName)
        {
            ICriteria criteria = Session.CreateCriteria(typeof(HouseHold));
            criteria.CreateAlias("HouseDistrict", "h").Add(Expression.Like("h.Name", "%" + houseDistrictName + "%"))
                .Add(Expression.Eq("Enable", true));
            IList<IHouseHold> houseHoldList = criteria.List<IHouseHold>();
            object[] personObjList = (from o in houseHoldList
                                      where o.Enable == true
                                      select new
                                      {
                                          Name = o.Name,
                                          Number = o.Number,
                                          Description = o.Description,
                                          Id = o.Id,
                                          Owner = (from w in o.Persons
                                                   where w.PersonType != null && w.Enable == true && w.PersonType.Name == "房主"
                                                   select w.Name).FirstOrDefault(),
                                          Leader = (from p in o.Persons
                                                    where p.PersonType != null && p.Enable == true && p.PersonType.Name == "负责人"
                                                    select p.Name).FirstOrDefault(),
                                          CreateDate = o.CreateDate,
                                          HouseDistrict = o.HouseDistrict == null ? "" : o.HouseDistrict.Name,
                                          Enable = o.Enable,
                                          Purpose = string.Join(",", (from p in o.Purposes
                                                                      where p.Enable = true
                                                                      select p.Name).ToArray<string>())
                                      }).ToArray();
            return personObjList;
        }

        public override void Update(IHouseHold entity)
        {
            ITransaction transaction = null;
            ExecuteTransaction((ITransaction tran) =>
            {

                entity.ModifyDate = DateTime.Now;
                entity.Enable = true;
                Session.Merge(entity);
            }, transaction);
        }
    }
}
